package com.ruibang.glass.equipment.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruibang.glass.common.constants.RedisKey;
import com.ruibang.glass.equipment.constant.Constants;
import com.ruibang.glass.equipment.domain.req.device.DeviceInfoReq;
import com.ruibang.glass.equipment.entity.device.DeviceChild;
import com.ruibang.glass.equipment.entity.device.EquipmentDeviceInfo;
import com.ruibang.glass.equipment.mapper.GEquipmentDeviceInfoMapper;
import com.ruibang.glass.equipment.service.DeviceChildService;
import com.ruibang.glass.equipment.service.EquipmentDeviceInfoService;
import com.teaming.cloud.framework2.autoconfigure.redis.RedisSingleService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 设备-设备管理 服务实现类
 * </p>
 *
 * @author mameng
 * @since 2024-04-07
 */
@Service
public class EquipmentDeviceInfoServiceImpl extends ServiceImpl<GEquipmentDeviceInfoMapper, EquipmentDeviceInfo> implements EquipmentDeviceInfoService {

    @Autowired
    private DeviceChildService deviceChildService;

    @Autowired
    private RedisSingleService redisService;

    /**
     * 获取设备管理列表
     * @param deviceInfoReq
     * @return
     */
    @Override
    public IPage<EquipmentDeviceInfo> getPage(DeviceInfoReq deviceInfoReq) {
        LambdaQueryWrapper<EquipmentDeviceInfo> queryWrapper = Wrappers.<EquipmentDeviceInfo>lambdaQuery();
        queryWrapper.eq(EquipmentDeviceInfo::getIsDelete, Constants.ZERO_STR);
        if (StringUtils.isNotBlank(deviceInfoReq.getDeviceCode())) {
            queryWrapper.like(EquipmentDeviceInfo::getDeviceCode, deviceInfoReq.getDeviceCode());
        }
        if (StringUtils.isNotBlank(deviceInfoReq.getDeviceName())) {
            queryWrapper.like(EquipmentDeviceInfo::getDeviceName, deviceInfoReq.getDeviceName());
        }
        if (StringUtils.isNotBlank(deviceInfoReq.getProductLine())) {
            queryWrapper.like(EquipmentDeviceInfo::getProductLine, deviceInfoReq.getProductLine());
        }
        if (StringUtils.isNotBlank(deviceInfoReq.getWorkingProcedure())) {
            queryWrapper.like(EquipmentDeviceInfo::getWorkingProcedure, deviceInfoReq.getWorkingProcedure());
        }
        if (StringUtils.isNotBlank(deviceInfoReq.getStatus())) {
            queryWrapper.like(EquipmentDeviceInfo::getStatus, deviceInfoReq.getStatus());
        }
        if (StringUtils.isNotBlank(deviceInfoReq.getWarehouseId())) {
            queryWrapper.like(EquipmentDeviceInfo::getWarehouseId, deviceInfoReq.getWarehouseId());
        }
        IPage<EquipmentDeviceInfo> page = baseMapper.selectPage(new Page<>(deviceInfoReq.getPageNo(), deviceInfoReq.getPageSize()),
                queryWrapper.orderByDesc(EquipmentDeviceInfo::getCreateTime));
        Map<Object, Object> workshopMap = redisService.hmget(RedisKey.WORKSHOP_GROUP_TYPE);
        if(page!=null&&CollectionUtils.isNotEmpty(page.getRecords())){
            for (EquipmentDeviceInfo record : page.getRecords()) {
                if(workshopMap!=null){
                    record.setWarehouseName(workshopMap.get(record.getWarehouseId())==null?null:workshopMap.get(record.getWarehouseId()).toString());
                }

            }
        }

        return page;
    }
    /**
     * 新增设备
     * @param equipmentDeviceInfo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addEquipmentDeviceInfo(EquipmentDeviceInfo equipmentDeviceInfo) {
        baseMapper.insert(equipmentDeviceInfo);
        if (!StringUtils.isEmpty(equipmentDeviceInfo.getDeviceId())) {
            List<DeviceChild> deviceChildren = deviceChildService.list(Wrappers.<DeviceChild>lambdaQuery().eq(DeviceChild::getDeviceId, equipmentDeviceInfo.getDeviceId()));
            for (DeviceChild deviceChild : deviceChildren) {
                deviceChild.setDeviceId(equipmentDeviceInfo.getId());
            }
            deviceChildService.updateBatchById(deviceChildren);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void removeAll(EquipmentDeviceInfo equipmentDeviceInfo) {
        deviceChildService.remove(Wrappers.<DeviceChild>lambdaQuery().in(DeviceChild::getDeviceId, equipmentDeviceInfo.getIds()));
        baseMapper.deleteBatchIds(equipmentDeviceInfo.getIds());
    }

}
